home *** CD-ROM | disk | FTP | other *** search
/ Developer Helper 1: Phil & Dave's Excellent CD / Excellent CD HFS.raw / Moof / Goodies / HyperCard Goodies / HyperCard Dev. ToolKit / Serial & MacinTalk XCMDs / recvUpTo.p < prev    next >
Text File  |  1987-07-15  |  4KB  |  145 lines

  1. {$R-}
  2.  
  3. (*
  4.     recvUpTo(port number, termination character, waitTime, echo, edit) -- Return a string from the
  5.         serial port; return everything available, up to the termination character (if any). Pass an empty
  6.         termination character to receive everything available. WaitTime is the amount of time to wait
  7.         for the input, in ticks (60ths of a second). Echo is true to enable echoing. Edit is
  8.         true to enable edit characters (i.e., backspace).
  9.  
  10.     To compile and link this file using Macintosh Programmer's Workshop,
  11.  
  12.     pascal -w recvUpTo.p
  13.     link -m ENTRYPOINT -o HyperCommands -rt XFCN=0 -sn Main=recvUpTo recvUpTo.p.o "{MPW}"Libraries:interface.o
  14.     
  15. *)
  16.  
  17. {$S recvUpTo }     { Segment name must be the same as the command name. }
  18.  
  19. unit DummyUnit;
  20.  
  21. interface
  22.  
  23. uses MemTypes, QuickDraw, OSIntf, ToolIntf, HyperXCmd;
  24.  
  25. procedure EntryPoint(paramPtr: XCmdPtr);
  26.     
  27. implementation
  28.  
  29. const
  30.  
  31. return = chr(13);
  32. linefeed = chr(10);
  33. bs = chr(8);
  34.  
  35. type
  36.  
  37. Str31 = String[31];
  38.  
  39. procedure recvUpTo(paramPtr: XCmdPtr); forward;
  40.  
  41. procedure EntryPoint(paramPtr: XCmdPtr);
  42.  
  43.     begin
  44.         recvUpTo(paramPtr);
  45.     end;
  46.  
  47. procedure recvUpTo(paramPtr: XCmdPtr);
  48.  
  49.     var portNumber: integer;
  50.         inPort, outPort: integer;
  51.         str: Str255;
  52.         l: longInt;
  53.         waitForChars: longInt;
  54.         lookForTerm: boolean;
  55.         termChar: char;
  56.         echoOn: boolean;
  57.         editOn: boolean;
  58.         linefeedStr: string[1];
  59.         bsStr: string[3];
  60.  
  61.     {$I XCmdGlue.inc}
  62.  
  63.     procedure Fail(errMsg: Str255); { set theResult and quit }
  64.         begin
  65.             paramPtr^.returnValue := PasToZero(errMsg);
  66.             exit(recvUpTo);
  67.         end;
  68.  
  69.     begin
  70.         if paramPtr^.paramCount <> 5 then Fail('parameter count is not 5');
  71.  
  72.         ZeroToPas(paramPtr^.params[1]^,str);        { First parameter is port number. }
  73.         portNumber := StrToNum(str);
  74.         if (portNumber < 1) or (portNumber > 2) then Fail('invalid port number');
  75.         ZeroToPas(paramPtr^.params[2]^,str);        { Second parameter is termination character. }
  76.         if length(str) = 0 then lookForTerm := false
  77.         else
  78.             begin
  79.                 lookForTerm := true;
  80.                 termChar := str[1];
  81.             end;
  82.         ZeroToPas(paramPtr^.params[3]^,str);        { Third parameter is whether to wait. }
  83.         waitForChars := StrToNum(str);
  84.         ZeroToPas(paramPtr^.params[4]^,str);        { Fourth parameter is whether to echo. }
  85.         echoOn := false;
  86.         if length(str) > 0 then
  87.             if (str[1] = 't') or (str[1] = 'T') then echoOn := true;
  88.         ZeroToPas(paramPtr^.params[5]^,str);        { Fifth parameter is whether to edit. }
  89.         editOn := false;
  90.         if length(str) > 0 then
  91.             if (str[1] = 't') or (str[1] = 'T') then editOn := true;
  92.  
  93.         if portNumber = 1 then
  94.             begin
  95.                 inPort := -6;
  96.                 outPort := -7;
  97.             end
  98.         else
  99.             begin
  100.                 inPort := -8;
  101.                 outPort := -9;
  102.             end;
  103.         linefeedStr[0] := chr(1); linefeedStr[1] := linefeed;
  104.         bsStr := '   '; bsStr[1] := bs; bsStr[3] := bs;
  105.         str := '';
  106.         waitForChars := waitForChars + TickCount;
  107.         while TickCount <= waitForChars do
  108.             begin
  109.                 if SerGetBuf(inPort,l) <> noErr then Fail('SerGetBuf failed');
  110.                 if l = 0 then cycle;
  111.                 str[0] := chr(integer(str[0])+1);
  112.                 l := 1;
  113.                 if FSRead(inPort,l,Ptr(ord4(@str)+ord(str[0]))) <> noErr then Fail('FSRead failed');
  114.                 if echoOn then
  115.                     begin
  116.                         l := 1;
  117.                         if FSWrite(outPort,l,Ptr(ord4(@str)+ord(str[0]))) <> noErr then Fail('FSWriter failed');
  118.                         if str[length(str)] = return then
  119.                             begin
  120.                                 l := length(linefeedStr);
  121.                                 if FSWrite(outPort,l,Ptr(ord4(@linefeedStr)+1)) <> noErr then Fail('FSWrite failed');
  122.                             end;
  123.                         if editOn and (str[length(str)] = bs) then
  124.                             begin
  125.                                 l := length(bsStr);
  126.                                 if FSWrite(outPort,l,Ptr(ord4(@bsStr)+1)) <> noErr then Fail('FSWrite failed');
  127.                             end;
  128.                     end;
  129.                 if editOn then
  130.                     begin
  131.                         if str[length(str)] = bs then
  132.                             begin
  133.                                 str[0] := chr(length(str)-1);
  134.                                 if length(str) > 0 then str[0] := chr(length(str)-1);
  135.                             end;
  136.                     end;
  137.                 if lookForTerm then
  138.                     if str[length(str)] = termChar then leave;
  139.                 if length(str) = 255 then leave;
  140.             end;
  141.         paramPtr^.returnValue := PasToZero(str);
  142.     end;
  143.  
  144. end.
  145.